package cn.doitedu.javase.thread;

import java.util.Random;

public class _02_线程同步 {

    static Integer count = 0;
    static Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(new Adder());
        Thread t2 = new Thread(new Adder());

        t1.start();
        t2.start();

        t1.join();
        t2.join();


        System.out.println(count);
    }


    static class Adder implements Runnable{

        Random random = new Random();

        @Override
        public void run() {
            System.out.println(random.nextInt(100));

            synchronized (lock) {
                for(int i=0;i<5000;i++) {
                    count++;
                }
            }

        }
    }


}
